package controller.main
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import model.monster.MonsterModel;
	import model.weapon.WeaponModel;
	
	import org.robotlegs.mvcs.StarlingCommand;
	
	import service.SQLService;
	
	import util.AtlasTextureGenerator;
	
	import view.event.main.LoadViewEvent;
	
	
	/**
	 * 进入程序首次加载数据
	 * @author £iang
	 * 
	 */	
	public class LoadFirstDataCommand extends StarlingCommand
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		[Inject]
		public var monsterModel:MonsterModel;
		[Inject]
		public var weaponModel:WeaponModel;
		[Inject]
		public var sqlService:SQLService;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 步骤计时器 */
		private var _timer:Timer;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 执行函数 
		 * 
		 */		
		override public function execute():void
		{
			_timer = new Timer( 800 );
			_timer.addEventListener( TimerEvent.TIMER, stepHandler );
			_timer.start();
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理步骤计时中派发的事件 
		 * @param event 步骤计时中派发的事件
		 * 
		 */		
		protected function stepHandler( event:TimerEvent ):void
		{
			var loadViewEvent:LoadViewEvent;
			
			switch(_timer.currentCount)
			{
				case 1:
				{
					//加载图集
					_timer.stop();
					AtlasTextureGenerator.initialize();
					loadViewEvent = 
						new LoadViewEvent( LoadViewEvent.STEP_LOAD_COMPLETE );
					dispatch( loadViewEvent );
					_timer.start();
					break;
				}
					
				case 2:
				{
					//连接数据库
					_timer.stop();
					sqlService.linkDataBase();
					loadViewEvent = 
						new LoadViewEvent( LoadViewEvent.STEP_LOAD_COMPLETE );
					dispatch( loadViewEvent );
					_timer.start();
					break;
				}
					
				case 3:
				{
					//加载怪物情报数据
					_timer.stop();
					monsterModel.initData();
					loadViewEvent = 
						new LoadViewEvent( LoadViewEvent.STEP_LOAD_COMPLETE );
					dispatch( loadViewEvent );
					_timer.start();
					break;
				}
					
				case 4:
				{
					//加载武器、防具情报数据
					_timer.stop();
					weaponModel.initData();
					loadViewEvent = 
						new LoadViewEvent( LoadViewEvent.STEP_LOAD_COMPLETE );
					dispatch( loadViewEvent );
					_timer.start();
					break;
				}
					
				case 5:
				{
					_timer.stop();
					loadViewEvent = 
						new LoadViewEvent( LoadViewEvent.STEP_LOAD_COMPLETE );
					dispatch( loadViewEvent );
					_timer.start();
					break;
				}
				
				case 6:
				{
					loadViewEvent = 
						new LoadViewEvent( LoadViewEvent.START_WIKI );
					dispatch( loadViewEvent );
					break;
				}
					
				default:
				{
					completeHandler();
					break;
				}
			}
		}
		
		/**
		 * 处理计时结束后派发的事件 
		 * @param event  计时结束后派发的事件 
		 * 
		 */		
		protected function completeHandler( event:TimerEvent = null ):void
		{
			_timer.removeEventListener( TimerEvent.TIMER, stepHandler );
			_timer.reset();
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
	}
}